package com.atguigu.controller;

import com.atguigu.client.SeckillFeignClient;
import com.atguigu.result.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

@Controller
public class WebSecKillController {
    @Autowired
    private SeckillFeignClient seckillFeignClient;

    //1.秒杀商品列表显示
    @GetMapping("/seckill-index.html")
    public String seckilIndex(Model model){
        //通过远程调用 查询秒杀商品列表信息
        RetVal retVal = seckillFeignClient.queryAllSecKillProduct();
        model.addAttribute("list",retVal.getData());
        return "seckill/index";
    }
    //2.秒杀商品详情页面
    @GetMapping("/seckill-detail/{skuId}.html")
    public String seckilDetail(@PathVariable Long skuId, Model model){
        //通过远程调用 查询秒杀商品列表信息
        RetVal retVal = seckillFeignClient.querySeckillProductById(skuId);
        model.addAttribute("item",retVal.getData());
        return "seckill/detail";
    }
    //3.获取抢购码成功之后的页面
    @GetMapping("/seckill-queue.html")
    public String seckilQueue(Long skuId,String seckillCode,  Model model){
        model.addAttribute("skuId",skuId);
        model.addAttribute("seckillCode",seckillCode);
        return "seckill/queue";
    }
    //4.秒杀订单确认页面
    @GetMapping("/seckill-confirm.html")
    public String seckilConfirm(Long skuId,String seckillCode,  Model model){
        RetVal retVal = seckillFeignClient.seckillConfirm();
        if(retVal.isOk()){
            Map<String,Object> retMap = (Map<String,Object>)retVal.getData();
            model.addAllAttributes(retMap);
        }else{
            model.addAttribute("message",retVal.getMessage());
        }
        return "seckill/confirm";
    }









}
